package dhm.com.hpx.activity;

import android.app.Service;
import android.os.Vibrator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.IndustryAdapter;
import dhm.com.hpx.adapter.IndustryMoreAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.GetIndustryBean;
import dhm.com.hpx.entity.GetIndustryFollowBean;
import dhm.com.hpx.utils.SpUtils;


public class EditActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.edit)
    RecyclerView mRecyclerView;
    @BindView(R.id.more)
    RecyclerView more;

    private PressenterImpl pressenter;
    private String uid;
    private List<GetIndustryFollowBean.DataBean> data1;
    private IndustryAdapter adapter;

    String type="1";
    private int fromPosition;
    private int toPosition;

    @Override
    protected int getLayout() {
        return R.layout.activity_edit;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        type= getIntent().getStringExtra("industryType");
        uid = SpUtils.getString(this, "uid");

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("type",type);
        map.put("user_id",uid);
        pressenter.sendMessage(this,Constant.list_industry_follow,map, GetIndustryFollowBean.class);

    }

    @Override
    protected void initData() {

    }


    ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int dragFrlg = 0;
            if (recyclerView.getLayoutManager() instanceof GridLayoutManager){
                dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN|ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;
            }else if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
                dragFrlg = ItemTouchHelper.UP|ItemTouchHelper.DOWN;
            }
            return makeMovementFlags(dragFrlg,0);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            //滑动事件 下面注释的代码，滑动后数据和条目错乱，被舍弃
//   Collections.swap(datas,viewHolder.getAdapterPosition(),target.getAdapterPosition());
//   ap.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());

            //得到当拖拽的viewHolder的Position
            fromPosition = viewHolder.getAdapterPosition();
            //拿到当前拖拽到的item的viewHolder
            toPosition = target.getAdapterPosition();
            /*Map<String,String> map=new HashMap<>();
            map.put("token",Constant.TOKEN);
            map.put("type",type);
            map.put("user_id",uid);
            map.put("industry_id",data1.get(fromPosition).getIndustry_id()+"");
            map.put("sort",(toPosition+1)+"");
            pressenter.sendMessage(EditActivity.this,Constant.modify_industry_follow,map, Bean.class);*/

            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(data1, i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(data1, i, i - 1);
                }
            }
            adapter.notifyItemMoved(fromPosition, toPosition);
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            //侧滑删除可以使用；
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }
        /**
         * 长按选中Item的时候开始调用
         * 长按高亮
         * @param viewHolder
         * @param actionState
         */
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                //获取系统震动服务//震动70毫秒
                Vibrator vib = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
                vib.vibrate(70);
            }
            super.onSelectedChanged(viewHolder, actionState);
        }

        /**
         * 手指松开的时候还原高亮
         * @param recyclerView
         * @param viewHolder
         */
        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            int sort_type=0;
            if (toPosition>fromPosition){
                sort_type=1;
            }else {
                sort_type=0;
            }
            Map<String,String> map=new HashMap<>();
            map.put("token",Constant.TOKEN);
            map.put("type",type);
            map.put("user_id",uid);
            map.put("industry_id",data1.get(toPosition).getIndustry_id()+"");
            map.put("sort",toPosition+"");
            map.put("sort_type",sort_type+"");
            pressenter.sendMessage(EditActivity.this,Constant.modify_industry_follow,map, Bean.class);
            adapter.notifyDataSetChanged(); //完成拖动后刷新适配器，这样拖动后删除就不会错乱
        }
    });

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetIndustryFollowBean){
            GetIndustryFollowBean getIndustryFollowBean= (GetIndustryFollowBean) data;
            if (getIndustryFollowBean.getCode()==1){
                data1 = getIndustryFollowBean.getData();
                adapter = new IndustryAdapter(this);
                GridLayoutManager manager=new GridLayoutManager(this,4);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                mRecyclerView.setLayoutManager(manager);
                adapter.setdeleteClick(new IndustryAdapter.OnClick() {
                    @Override
                    public void item(int posttion) {
                        Map<String,String> map=new HashMap<>();
                        map.put("token",Constant.TOKEN);
                        map.put("type",type);
                        map.put("user_id",uid);
                        map.put("industry_id",data1.get(posttion).getIndustry_id()+"");
                        pressenter.sendMessage(EditActivity.this,Constant.del_industry_follow,map, Bean.class);
                    }
                });
                mRecyclerView.setAdapter(adapter);
                helper.attachToRecyclerView(mRecyclerView);
                adapter.setList(data1);

            }


            Map<String,String> map=new HashMap<>();
            map.put("token", Constant.TOKEN);
            map.put("type",type);
            map.put("user_id",uid);
            pressenter.sendMessage(this,Constant.industry_list,map, GetIndustryBean.class);
        }else if (data instanceof GetIndustryBean){
            final GetIndustryBean getIndustryBean= (GetIndustryBean) data;
            if (getIndustryBean.getCode()==1){
                more.setVisibility(View.VISIBLE);
                GridLayoutManager manager=new GridLayoutManager(this,4);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                more.setLayoutManager(manager);
                IndustryMoreAdapter industryMoreAdapter=new IndustryMoreAdapter(this);
                industryMoreAdapter.setdeleteClick(new IndustryMoreAdapter.OnClick() {
                    @Override
                    public void item(int posttion) {
                        Map<String,String> map=new HashMap<>();
                        map.put("token",Constant.TOKEN);
                        map.put("type",type);
                        map.put("user_id",uid);
                        map.put("industry_id",getIndustryBean.getData().get(posttion).getCatId()+"");
                        map.put("industry_name",getIndustryBean.getData().get(posttion).getCatName()+"");
                        pressenter.sendMessage(EditActivity.this,Constant.add_industry_follow,map, Bean.class);
                    }
                });
                more.setAdapter(industryMoreAdapter);
                industryMoreAdapter.setList(getIndustryBean.getData());


            }else {
                more.setVisibility(View.GONE);
            }
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            if (bean.getCode()==1){
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("type",type);
                map.put("user_id",uid);
                pressenter.sendMessage(this,Constant.list_industry_follow,map, GetIndustryFollowBean.class);
            }
        }
    }

    @Override
    public void fail(String error) {

    }

}
